/***************************************************************************************************
 *Copyright(C),2010-2017,Sumscope
 *FileName	:  MacroIndexDict.h
 *Author	:  scofined.qi
 *Version	:  1.0
 *Date		:  2017/04/19
 *Desc		:  宏观指标字典的数据存储
 *Relation :
 *Others	:  //其他内容说明
 *Function :  //主要函数列表，每条记录应包含函数名及功能简要说明
 *History	:
 * //修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简介
 ***************************************************************************************************/
#pragma once

#include <core/Utility.h>
#include <string.h>

#include <map>
#include <set>
#include <string>

#include "DictType.h"
#include "HTable/HTable.h"

namespace qb {
namespace ht {
class MacroIndexManager;
class MACRO_INDEX {
 public:
  char Code[32];
  double Value;
  int Date;
  int EndDate;
  int Sequence;  // 变更序列号
  char Flag;
  bool operator<(const MACRO_INDEX& obj) const {
    if (Date < obj.Date)
      return true;
    else
      return false;
  }
  MACRO_INDEX& operator=(const MACRO_INDEX& rhs) {
    FIELDCOPY(Code, rhs.Code);
    Value = rhs.Value;
    Date = rhs.Date;
    EndDate = rhs.EndDate;
    Flag = rhs.Flag;
    Sequence = rhs.Sequence;
    return *this;
  }
  bool operator==(const MACRO_INDEX& rhs) const {
    return ((strcmp(Code, rhs.Code) == 0) && Value == rhs.Value &&
            Date == rhs.Date && EndDate == rhs.EndDate && Flag == rhs.Flag);
  }
};

class MacroIndexDict {
  typedef MACRO_INDEX Element;

 public:
  typedef std::set<Element> MacroIndexSet;
  typedef std::map<std::string, MacroIndexSet> MacroIndexSetMap;

 protected:
  MacroIndexSetMap m_macroindex;
  std::string m_md5;
  int m_updateid;
  MacroIndexManager* m_macroindexv2;
  double m_times[2];
  bool m_benchlog;  // 为true,则顺便开启旧实现,以便对比性能
  friend class DictTable;

 public:
  MacroIndexDict();
  ~MacroIndexDict();
  void UpdateState(const char* md5, int updateid);
  int GetCount() const;
  const std::string& GetMD5() const { return m_md5; }
  int GetUpdateId() const { return m_updateid; }
  bool UpdateElement(const DICT_MACRO_INDEX& elemval);
  void Dump();
  void Check();
  int UpdateTable(qb::ht::Table* table, bool sync);

 protected:
  int CalcDateDiff(int datestart, int dateend);
  int DateAdd(int date, int delta);
  void UpdateMacroElement(MacroIndexSet& objset, MACRO_INDEX& elemval);
};

}  // namespace ht
}  // namespace qb
